package com.hx.er.action.admin.settings;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErAdminAction;
import com.hx.er.bean.base.CountryBean;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErProperties;
import com.hx.er.service.admin.IAdminSettingsService;

public class AdminSettingsAction extends ErAdminAction {

	// 序列化ID
	private static final long serialVersionUID = -8686939949810369580L;

	private String loginFailureCount;
	private String pageSize;
	private String securityCodeLength;
	private String useScurityCode;
	private String defaultDateFormat;
	private String defaultCountry;
	private String cookieExpirationDate;

	private List<String> loginFailureCountList = new ArrayList<String>();
	private List<String> pageSizeList = new ArrayList<String>();
	private List<String> securityCodeLengthList = new ArrayList<String>();
	private List<String> useScurityCodeList = new ArrayList<String>();
	private List<String> defaultDateFormatList = new ArrayList<String>();
	private List<CountryBean> defaultCountryList = new ArrayList<CountryBean>();
	private List<String> cookieExpirationDateList = new ArrayList<String>();

	@Autowired
	private IAdminSettingsService adminSettingsService;

	public String init() {
		this.loginFailureCount = ErProperties.getProperty(Constants.PROPERTY_LIMIT_LOGIN_FAILURE_COUNT);
		this.pageSize = ErProperties.getProperty(Constants.PROPERTY_PAGE_SIZE);
		this.securityCodeLength = ErProperties.getProperty(Constants.PROPERTY_SECURITY_CODE_LENGTH);
		this.useScurityCode = ErProperties.getProperty(Constants.PROPERTY_USE_SCURITY_CODE);
		this.defaultDateFormat = ErProperties.getProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT);
		this.defaultCountry = ErProperties.getProperty(Constants.PROPERTY_DEFAULT_COUNTRY);
		this.cookieExpirationDate = ErProperties.getProperty(Constants.PROPERTY_COOKIE_EXPIRATION_DATE);

		// 初始化select列表
		for (int i = 0; i < 10; i++) {
			this.loginFailureCountList.add(String.valueOf(i + 1));
		}

		for (int i = 0; i < 10; i++) {
			this.pageSizeList.add(String.valueOf(i * 5 + 5));
		}

		for (int i = 4; i < 7; i++) {
			this.securityCodeLengthList.add(String.valueOf(i));
		}

		this.useScurityCodeList.add(Constants.BOOLEAN_STR_TRUE);
		this.useScurityCodeList.add(Constants.BOOLEAN_STR_FALSE);

		this.defaultDateFormatList.add("yyyy-MM-dd HH:mm:ss");
		this.defaultDateFormatList.add("yy-MM-dd HH:mm:ss");
		this.defaultDateFormatList.add("yy-MM-dd");
		this.defaultDateFormatList.add("MM-dd-yy HH:mm:ss");
		this.defaultDateFormatList.add("MM-dd-yy");
		this.defaultDateFormatList.add("dd-MM-yy HH:mm:ss");
		this.defaultDateFormatList.add("dd-MM-yy");

		this.defaultCountryList = this.adminSettingsService.getCountryList(null);
		
		this.cookieExpirationDateList.add("3");
		this.cookieExpirationDateList.add("7");
		this.cookieExpirationDateList.add("14");
		this.cookieExpirationDateList.add("30");

		return SUCCESS;
	}

	public String save() {
		// TODO 目前只是在服务器不重启的情况下更新
		ErProperties.setProperty(Constants.PROPERTY_LIMIT_LOGIN_FAILURE_COUNT, this.loginFailureCount);
		ErProperties.setProperty(Constants.PROPERTY_PAGE_SIZE, this.pageSize);
		ErProperties.setProperty(Constants.PROPERTY_SECURITY_CODE_LENGTH, this.securityCodeLength);
		// ErProperties.setProperty(Constants.PROPERTY_USE_SCURITY_CODE,
		// this.useScurityCode);
		ErProperties.setProperty(Constants.PROPERTY_COMMON_DATETIME_FORMAT, this.defaultDateFormat);
		ErProperties.setProperty(Constants.PROPERTY_DEFAULT_COUNTRY, this.defaultCountry);
		ErProperties.setProperty(Constants.PROPERTY_COOKIE_EXPIRATION_DATE, this.cookieExpirationDate);

		this.addActionMessage(super.getText(EDIT_SUCCESS_MSG));

		return SUCCESS;
	}

	public String getLoginFailureCount() {
		return loginFailureCount;
	}

	public void setLoginFailureCount(String loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getSecurityCodeLength() {
		return securityCodeLength;
	}

	public void setSecurityCodeLength(String securityCodeLength) {
		this.securityCodeLength = securityCodeLength;
	}

	public String getUseScurityCode() {
		return useScurityCode;
	}

	public void setUseScurityCode(String useScurityCode) {
		this.useScurityCode = useScurityCode;
	}

	public String getDefaultDateFormat() {
		return defaultDateFormat;
	}

	public void setDefaultDateFormat(String defaultDateFormat) {
		this.defaultDateFormat = defaultDateFormat;
	}

	public List<String> getLoginFailureCountList() {
		return loginFailureCountList;
	}

	public List<String> getPageSizeList() {
		return pageSizeList;
	}

	public List<String> getSecurityCodeLengthList() {
		return securityCodeLengthList;
	}

	public List<String> getUseScurityCodeList() {
		return useScurityCodeList;
	}

	public List<String> getDefaultDateFormatList() {
		return defaultDateFormatList;
	}

	public String getDefaultCountry() {
		return defaultCountry;
	}

	public void setDefaultCountry(String defaultCountry) {
		this.defaultCountry = defaultCountry;
	}

	public List<CountryBean> getDefaultCountryList() {
		return defaultCountryList;
	}

	public void setDefaultCountryList(List<CountryBean> defaultCountryList) {
		this.defaultCountryList = defaultCountryList;
	}

	public String getCookieExpirationDate() {
		return cookieExpirationDate;
	}

	public void setCookieExpirationDate(String cookieExpirationDate) {
		this.cookieExpirationDate = cookieExpirationDate;
	}

	public List<String> getCookieExpirationDateList() {
		return cookieExpirationDateList;
	}

}
